// 导入tokenManager
const tokenManager = require('../../utils/tokenManager');

Page({
  data: {
    userId: '', // 用户ID
    testVersions: [
      {
        id: 1,
        name: "简洁版",
        questionCount: 28,
        description: "仅需5分钟，快速了解你的MBTI类型"
      },
      {
        id: 2,
        name: "通用版",
        questionCount: 48,
        description: "需要10分钟，获得更准确的测试结果"
      },
      {
        id: 3,
        name: "完整版",
        questionCount: 93,
        description: "需要15分钟，提供最全面详尽的分析"
      }
    ]
  },
  
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: 'MBTI人格测试'
    });
    // 获取用户ID
    this.getUserId();
  },
  
  onShow: function() {
    // 页面显示时重新获取用户ID
    this.getUserId();
  },
  
  // 获取用户ID
  getUserId: function() {
    const app = getApp();
    let userId = "";
    
    // 尝试从全局获取用户ID
    if (app.globalData && app.globalData.userInfo && app.globalData.userInfo.userId) {
      userId = app.globalData.userInfo.userId;
    } else {
      // 从tokenManager获取用户信息
      const userInfo = tokenManager.getUserInfo();
      if (userInfo && userInfo.userId) {
        userId = userInfo.userId;
      } else {
        // 尝试从存储中获取
        try {
          const storedUserInfo = wx.getStorageSync('userInfo');
          if (storedUserInfo && storedUserInfo.userId) {
            userId = storedUserInfo.userId;
          }
        } catch(e) {
          console.error('获取用户信息失败:', e);
        }
      }
    }
    
    if (userId) {
      this.setData({
        userId: userId
      });
      console.log('获取到用户ID:', userId);
    } else {
      console.log('无法获取用户ID，将在测试时生成临时ID');
    }
  },
  
  startTest: function(e) {
    const versionIndex = e.currentTarget.dataset.version;
    const versionId = this.data.testVersions[versionIndex].id;
    
    wx.navigateTo({
      url: `/pages/mbti/question/question?version=${versionIndex}&versionId=${versionId}&userId=${this.data.userId}`
    });
  },
  
  onShareAppMessage: function() {
    return {
      title: '发现你的MBTI人格类型',
      path: '/pages/mbti/mbti'
    };
  }
}) 